package zhongbo.java.air.flightpath.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.MyException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.flightpath.entity.AirFlightPath;
import zhongbo.java.air.flightpath.mapper.AirFlightPathMapper;
import zhongbo.java.air.flightpath.service.AirFlightPathService;

import java.util.Date;
import java.util.UUID;

@Service
public class AirFlightPathServiceImpl implements AirFlightPathService {
    @Autowired
    private AirFlightPathMapper airFlightPathMapper;


    @Override
    public AirFlightPath addAirFlightPath(AirFlightPath airFlightPath) throws MyException {
        Date date = new Date();
        airFlightPath.setTimestamp(date);
        airFlightPath.setId(UUID.randomUUID().toString().substring(0, 8));
        if (!airFlightPath.getIcaoCode().matches("[A-Z]{6}")) {
            throw new MyException("ICAO码格式错误");
        }
        if (!airFlightPath.getSourceType().matches("(ADS_B|RADAR|MLAT)")) {
            throw new MyException("数据源格式错误");
        }
        int ok = airFlightPathMapper.insert(airFlightPath);
        if (ok <= 0) {
            throw new MyException("添加失败");
        }
        return airFlightPathMapper.selectByIcaoCode(airFlightPath.getIcaoCode());
    }

    @Override
    public void deleteAirFlightPath(String icaoCode) throws MyException {
        if (!icaoCode.matches("[A-Z]{6}")) {
            throw new MyException("ICAO码格式错误");
        }
        int ok = airFlightPathMapper.deleteByIcaoCode(icaoCode);
        if (ok <= 0) {
            throw new MyException("删除失败");
        }

    }

    @Override
    public Integer updateAirFlightPath(AirFlightPath airFlightPath) throws MyException {
        if (!airFlightPath.getIcaoCode().matches("[A-Z]{6}")) {
            throw new MyException("ICAO码格式错误");
        }
        if (airFlightPath.getSourceType() != null) {
            if (!airFlightPath.getSourceType().matches("(ADS_B|RADAR|MLAT)")) {
                throw new MyException("数据源格式错误");
            }
        }
        Integer ok = airFlightPathMapper.updateAirFlightPath(airFlightPath, airFlightPath.getIcaoCode());
        if (ok <= 0) {
            throw new MyException("更新失败");
        }
        return ok;
    }

    @Override
    public AirFlightPath selectAirFlightPath(AirFlightPath airFlightPath) throws MyException {
        if (airFlightPath.getIcaoCode().matches("[A-Z]{6}")) {
            return airFlightPathMapper.selectAllAirFlightPath(airFlightPath);
        } else {
            throw new MyException("ICAO码格式错误");
        }
    }

    @Override
    public AirFlightPath SelectByIcaoCode(String icaoCode) throws MyException {
        if (icaoCode.matches("[A-Z]{6}")) {
            AirFlightPath airFlightPath = airFlightPathMapper.selectByIcaoCode(icaoCode);
            if (airFlightPath != null) {
                return airFlightPath;
            } else {
                throw new MyException("ICAO码不存在");
            }
        } else {
            throw new MyException("ICAO码格式错误");
        }
    }


    @Override
    public AirFlightPath updatePath(AirFlightPath airFlightPath) throws MyException {

        if (!airFlightPath.getIcaoCode().matches("[A-Z]{6}")) {
            throw new MyException("ICAO码格式错误");
        }
        if (airFlightPath.getSourceType() != null) {
            if (!airFlightPath.getSourceType().matches("(ADS_B|RADAR|MLAT)")) {
                throw new MyException("数据源格式错误");
            }
        }
        int ok = airFlightPathMapper.updateById(airFlightPath);
        if (ok <= 0) {
            throw new MyException("更新失败");
        }
        return airFlightPathMapper.selectByIcaoCode(airFlightPath.getIcaoCode());
    }

    @Override
    public PageVo<AirFlightPath> page(Integer pageNumber, Integer pageSize, boolean order, String searchField, String keyword, String sortField) {
        // 创建查询条件
        QueryWrapper<AirFlightPath> queryWrapper = new QueryWrapper<>();
        // 如果关键字和搜索字段都不为空，则添加模糊查询条件
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(searchField)) {
            queryWrapper.like(searchField, keyword);
        }
        // 如果排序字段不为空，则添加排序条件
        if (StrUtil.isNotBlank(sortField)) {
            // 如果order为true，则按升序排序，否则按降序排序
            if (order) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        }
        // 创建分页参数
        IPage<AirFlightPath> pageParam = new Page<>(pageNumber, pageSize);
        // 执行查询，并返回结果
        IPage<AirFlightPath> resultPage = airFlightPathMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(resultPage);
    }

    @Override
    public PageVo<AirFlightPath> pageByIcaoCode(Integer pageNumber, Integer pageSize, String icaoCode, String startTime, String endTime,String oderByField, boolean orderByAsc) throws MyException {
        QueryWrapper<AirFlightPath> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(icaoCode)) {
            queryWrapper.eq("icao_code", icaoCode);
        }
        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            queryWrapper.between("timestamp", startTime, endTime);
        }
        if(StrUtil.isNotBlank(oderByField)){
            if (orderByAsc) {
                queryWrapper.orderByAsc(oderByField);
            } else {
                queryWrapper.orderByDesc(oderByField);
            }
        }
        IPage<AirFlightPath> pageParam = new Page<>(pageNumber, pageSize);
        IPage<AirFlightPath> resultPage = airFlightPathMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(resultPage);
    }
}
